<template>
    <div class="main-content">
        <TableLayout form-title="">
            <div slot="form-title" class="table-title"></div>
            <el-form :model="query" ref="query" slot="form" label-width="75px" label-position="right">
                <el-row :gutter="41">
                    <el-col :span="8">
                        <el-form-item label="学校名称" prop="schoolName">
                            <school v-model="query.schoolName" style="width:100%" @change='schoolChange'></school>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="发放日期" prop="date">
                            <el-date-picker v-model="query.date" type="date" placeholder="日期" style="width:100%"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-button type="primary" class="top-btn" @click="searchData">查询</el-button>
                    </el-col>
                </el-row>
            </el-form>
            <div class="table-title" slot="table-title">
            </div>
            <el-table :data="tableData" border v-loading="loading" style="width: 100%">  
                <el-table-column label="学校名称" align="center">
                    <template scope="scope">
                        <span>{{scope.row.sSchoolName}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="商品名称" align="center">
                    <template scope="scope">
                        <span>{{scope.row.sProductName}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="发放批次" align="center">
                    <template scope="scope">
                        <span>{{scope.row.sBatchNumber}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="发放数量（张）" align="center">
                    <template scope="scope">
                        <span>{{scope.row.sNumber}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="发放时间" min-width="140" align="center">
                    <template scope="scope">
                        <span>{{ scope.row.sCreateTime | dateFilter}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" >
                    <template scope="scope">
                        <el-button type="text" class="btn-success" @click="goToCheck(scope.row)">查看</el-button>
                        <el-button type="text" v-if="scope.row.cancle!='0'" class="btn-success" @click="openRecord(scope.row.pId)">开通</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination layout="total, sizes, prev, pager, next, jumper" :total="pageQuery.total" 
                :current-page.sync="pageQuery.currentpage" :page-sizes="config.pageSizes" :page-size="config.pageSize" 
                @size-change="handleSizeChange" @current-change="handleCurrentChange">
                </el-pagination>
            </div>
        </TableLayout>
    </div>
</template>

<script>
    import moment from 'moment';
    import Levelbar from '../../layout/Levelbar';
    import TableLayout from 'components/TableLayout';
    import school from 'components/SchoolClasss/school';
    import {listCardCouponsGrantRecord, openServiceByBatchNumberId} from 'api/cardCoupons';
    import { mapState, mapActions } from 'vuex';
    export default {
        name: 'cardExtendList',
        components: { Levelbar, TableLayout, school },
        data() {
            return { 
                loading: true,  
                schoolLoading: false,
                tableData: [],
                pageQuery: {
                    currentpage: 1,
                    total: 0
                },
                query: {
                    schoolName: '',
                    schoolId: '',
                    date: ''
                }
            }
        },
        // watch: {// 局部刷新列表 2018-1-10 by cuihd
        //     cacheUpdated(val) {
        //         val && this.searchFetchData();
        //     }
        // },
        created() {
            this.queryData();
        },
        computed: {
            ...mapState({
                config: state => state.config.table
               // cacheUpdated: state => state.app.cacheUpdated// 局部刷新列表 2018-1-10 by cuihd
            })
        },
        filters: {
            dateFilter(val) {
                if (!val) {
                    return '--'
                } else {
                    return val.substring(0, 19)
                }
            }
        },
        methods: {
            ...mapActions(['ChangePageSize']),
            handleSizeChange(val) {
                this.pageQuery.currentpage = 1;
                this.ChangePageSize(val);
                this.queryData();
            },
            handleCurrentChange(val) {
                this.pageQuery.currentpage = val;
                this.queryData();
            },
            searchData() {
                this.pageQuery.currentpage = 1;
                this.queryData();
            },
            // 分页查询卡券发放列表
            queryData() {
                this.loading = true;
                let queryDate = this.query.date==''?'':moment(this.query.date).format('YYYY-MM-DD');
                listCardCouponsGrantRecord(this.pageQuery.currentpage, this.config.pageSize, this.query.schoolId, queryDate).then(response => {
                    this.tableData = response.data.content.list;
                    this.pageQuery.total = response.data.content.pagination.total;
                    this.loading = false;
                }).catch(Error => {
                    this.$message.error(Error);
                    this.tableData = [];
                    this.pageQuery.total = '0'
                    this.loading = false;
                })
                
            },
            goToCheck(row) {              
                this.$router.push({ path: `extendCheck/${row.pId}/${row.fSchoolId}` });
            },
            // 卡券开通(通过批次号id)
            openRecord(pid) {
                this.$confirm('该批次中已经使用的卡券不能开通，确定要开通该批次发放的所有未使用的卡券吗？', ' ', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                }).then(() => {
                    this.loading = true;
                    openServiceByBatchNumberId(pid).then(response => {
                        this.loading = false;
                        this.queryData(this.pageQuery.page);
                        this.$message({type: 'success', message: response.data.errorMsg});
                    }).catch(Error => {
                        this.loading = false;
                        // this.$message.error(Error);
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消开通'
                    });          
                });
            },
            // 获取学校id
            schoolChange(val) {
                this.query.schoolId = val
            }

        }

    }
</script>

<style lang="scss" scoped>

</style>

